# -*- coding: utf-8 -*-

import json
import regex  # 使用 regex 模块代替 re

def extract_json_from_string(input_data):
    """
    从输入字符串中提取并解析 JSON 内容。
    """
    # 检查输入是否为字符串
    if not isinstance(input_data, str):
        input_string = str(input_data)
    else:
        input_string = input_data
 
    # 去除可能存在的代码块标记
    input_string = regex.sub(r'```json\s*|\s*```', '', input_string).strip()

    # 使用递归正则表达式匹配 JSON 格式的内容
    json_pattern = r'\{(?:[^{}]|(?R))*\}'
    
    try:
        # 使用正则表达式查找 JSON 部分
        match = regex.search(json_pattern, input_string)
        if match:
            json_str = match.group(0)
            # 尝试解析 JSON 字符串
            parsed_json = json.loads(json_str)
            return parsed_json
        
        # 如果没有匹配到 JSON，返回错误信息
        return {"error": input_string}
    
    except json.JSONDecodeError as e:
        # 如果 JSON 解析失败，返回错误信息
        return {"error": f"JSON 解析失败: {str(e)}"}

# 测试用例
if __name__ == "__main__":
    print("正在运行主程序...")  # 调试信息
    test_cases = [
        "{\"error\": \"没有收到文件\"}",
        "```json \n {\"error\": \"没有收到文件\"} \n ```",
        "哈哈 好的 {\"error\": \"\\n没有收到文件\"}",
        "这是一个无效的字符串，没有 JSON。",
        "{\"key\": \"value\", \"number\": 123}",
        '我收到了音频文件，正在尝试识别和翻译。\n\n```json\n{\n  "source_language": "Tagalog",\n  "transcriptions": [\n    {\n      "start_time": "00s",\n      "end_time": "03s",\n      "original_text": "Meron ng tatlong clay stick ang Skintific.",\n      "translated_text": "Skintific现在有三种泥膜棒。"\n    },\n    {\n      "start_time": "03s",\n      "end_time": "07s",\n      "original_text": "We have pink, green, and gray, and pink is their newest one.",\n      "translated_text": "我们有粉色、绿色和灰色，粉色是他们最新的。"\n    },\n    {\n      "start_time": "07s",\n      "end_time": "22s",\n      "original_text": "Maraming beses ko na na-mention na yung acne clay stick or the green one is to deal with your active pimples since it contains salicylic, also has Centella, which is good for hydration, and it really comes down your acne.",\n      "translated_text": "我已经多次提到，祛痘泥膜棒或绿色款是用来处理你的活性痘痘的，因为它含有水杨酸，还含有积雪草，这对保湿有好处，而且它确实能缓解你的痘痘。"\n    },\n    {\n      "start_time": "22s",\n      "end_time": "26s",\n      "original_text": "Kumbaga para siya sa alagasgas.",\n      "translated_text": "就像是用来治疗皮疹一样。"\n    },\n    {\n      "start_time": "26s",\n      "end_time": "33s",\n      "original_text": "For their gray na version or yung kanilang pore clay stick, deals with deep cleaning your pores.",\n      "translated_text": "对于他们的灰色版本或他们的毛孔泥膜棒，是用来深层清洁你的毛孔的。"\n    },\n    {\n      "start_time": "33s",\n      "end_time": "38s",\n      "original_text": "Rerefine niya yung skin, gently exfoliate the dead layer.",\n      "translated_text": "它会细化你的皮肤，温和地去除死皮层。"\n    },\n    {\n      "start_time": "38s",\n      "end_time": "40s",\n      "original_text": "Pero para saan nga ba itong pink na version?",\n      "translated_text": "但是这个粉色版本是用来做什么的呢？"\n    },\n    {\n      "start_time": "40s",\n      "end_time": "45s",\n      "original_text": "This is Skintific\'s Niacinamide Bright Boost Clay Stick.",\n      "translated_text": "这是Skintific的烟酰胺亮肤泥膜棒。"\n    },\n    {\n      "start_time": "45s",\n      "end_time": "53s",\n      "original_text": "From the name itself, can really brighten your dark spots, kung meron kayong mga hyperpigmentations, acne marks.",\n      "translated_text": "从名字本身就可以看出，它真的可以提亮你的黑斑，如果你有色素沉淀、痘印。"\n    },\n    {\n      "start_time": "53s",\n      "end_time": "59s",\n      "original_text": "Since here ingredient niya yung niacinamide, which is very famous to really lighten and brighten up your face.",\n      "translated_text": "因为它的主要成分是烟酰胺，它非常有名，可以真正淡化和提亮你的脸部。"\n    },\n    {\n      "start_time": "59s",\n      "end_time": "01m05s",\n      "original_text": "Meron din \'tong arbutin, which is nakakatulong naman sa discoloration that is caused by UV or yung sa sikat ng araw.",\n      "translated_text": "它还含有熊果苷，这有助于解决由紫外线或阳光引起的肤色不均。"\n    },\n    {\n      "start_time": "01m05s",\n      "end_time": "01m10s",\n      "original_text": "Meron din siyang tranexamic acid, which is in charge to fade your dark spots.",\n      "translated_text": "它还含有氨甲环酸，负责淡化你的黑斑。"\n    },\n    {\n      "start_time": "01m10s",\n      "end_time": "01m13s",\n      "original_text": "Coloration, melasma.",\n      "translated_text": "色素沉淀，黄褐斑。"\n    },\n    {\n      "start_time": "01m13s",\n      "end_time": "01m20s",\n      "original_text": "So sa madaling salita, this will help you brighten your skin para lumabas yung glow, mas maging radiant.",\n      "translated_text": "简而言之，这将帮助你提亮你的皮肤，让你的肌肤焕发光彩，更加容光焕发。"\n    },\n    {\n      "start_time": "01m20s",\n      "end_time": "01m23s",\n      "original_text": "And I think they really perfected their clay sticks.",\n      "translated_text": "我认为他们真的完善了他们的泥膜棒。"\n    },\n    {\n      "start_time": "01m23s",\n      "end_time": "01m27s",\n      "original_text": "Just leave this for 15 to 20 minutes and I\'ll be back.",\n      "translated_text": "只需敷15到20分钟，我就会回来。"\n    },\n    {\n      "start_time": "01m27s",\n      "end_time": "01m29s",\n      "original_text": "I am now back.",\n      "translated_text": "我现在回来了。"\n    },\n    {\n      "start_time": "01m29s",\n      "end_time": "01m32s",\n      "original_text": "Nakapagbanlaw na ako ng face.",\n      "translated_text": "我已经洗完脸了。"\n    },\n    {\n      "start_time": "01m32s",\n      "end_time": "01m36s",\n      "original_text": "And my face feels more smooth.",\n      "translated_text": "我的脸感觉更光滑了。"\n    },\n    {\n      "start_time": "01m36s",\n      "end_time": "01m40s",\n      "original_text": "Masarap talaga sa pakiramdam after mong balawan yung clay stick ng Skintific.",\n      "translated_text": "洗掉Skintific的泥膜棒后，感觉真的很舒服。"\n    },\n    {\n      "start_time": "01m40s",\n      "end_time": "01m44s",\n      "original_text": "My face feels more refreshed.",\n      "translated_text": "我的脸感觉更清爽了。"\n    },\n    {\n      "start_time": "01m44s",\n      "end_time": "01m47s",\n      "original_text": "But going back to this product.",\n      "translated_text": "但回到这个产品。"\n    },\n    {\n      "start_time": "01m47s",\n      "end_time": "01m49s",\n      "original_text": "What about you guys?",\n      "translated_text": "你们觉得怎么样？"\n    },\n    {\n      "start_time": "01m49s",\n      "end_time": "01m53s",\n      "original_text": "Ano sa tingin ninyo yung kakailanganin ninyo from these three?",\n      "translated_text": "你们认为你们需要这三种中的哪一种？"\n    },\n    {\n      "start_time": "01m53s",\n      "end_time": "01m56s",\n      "original_text": "I just can\'t wait for you to try this.",\n      "translated_text": "我迫不及待地想让你们尝试一下。"\n    },\n    {\n      "start_time": "01m56s",\n      "end_time": "01m57s",\n      "original_text": "Go check it out.",\n      "translated_text": "去看看吧。"\n    }\n  ]\n}\n```'
    ]
    
    for i, case in enumerate(test_cases):
        print(f"测试用例 {i + 1}:")
        print("输入:", case)
        print("输出:", extract_json_from_string(case))
        print("-" * 40)